package com.test;

import java.io.*;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: denny
 * Date: 7/24/13
 * Time: 6:34 PM
 * To change this template use File | Settings | File Templates.
 */
public class NatLangProcExam {
  static class CMap extends HashMap<String,Integer>{
    void add(String s, int val){
      if( !containsKey(s) ){
        put(s,val);
      }else{
        put(s,val+get(s));
      }
    }
    int val0(String s ){
      if( containsKey(s)){
        return get(s);
      }
      return 0;
    }
  }

  CMap totalCounts = new CMap();
  CMap totalTagCounts = new CMap();

  CMap emitGene = new CMap();
  CMap emitO = new CMap();

  public static void main(String[] args) throws Exception{
    new NatLangProcExam().run();
  }
  
  void run() throws Exception{
    fillTotalCounts();
    fillEmits();
    tagIt();
  }
  
  String rareWord(String w){
    if( totalCounts.val0(w)<5 ){
      return "_RARE_";
    }
    return w;
  }
  
  double e(String x, String y){
    if( y.equals("O") ){
      return (double)emitO.val0(x)/totalTagCounts.val0(y);
    }
    if( y.equals("I-GENE") ){
      return (double)emitGene.val0(x)/totalTagCounts.val0(y);
    }
    throw new RuntimeException();
  }
  
  String tagFor(String w){
    //need function e(x|y) - divided by count(y) !!!!
    
    String r = rareWord(w);
    if( e(r,"O")>e(r,"I-GENE") ){
      return "O";
    }
    return "I-GENE";
  }

  private void fillEmits() throws Exception {
    File fcount = new File("/home/denny/proj/NLP/gene.counts");
    BufferedReader br = new BufferedReader(new FileReader(fcount));
    String line;

    while((line=br.readLine())!=null){
      String[] arr = line.split(" ");
      if(arr[1].equals("WORDTAG")){
        int c = Integer.parseInt(arr[0]);
        String tag = arr[2];
        String word = arr[3];
        if( tag.equals("I-GENE") ){
          emitGene.add(rareWord(word),c);
        }else if( tag.equals("O")){
          emitO.add(rareWord(word),c);
        }else{
          throw new Exception();
        }
        totalCounts.add(word, c);
      }
    }
  }

  private void fillTotalCounts() throws IOException {
    File fcount = new File("/home/denny/proj/NLP/gene.counts");
    BufferedReader br = new BufferedReader(new FileReader(fcount));
    String line;

    while((line=br.readLine())!=null){
      String[] arr = line.split(" ");
      if(arr[1].equals("WORDTAG")){
        int c = Integer.parseInt(arr[0]);
        String tag = arr[2];
        String word = arr[3];
        totalCounts.add(word, c);
        totalTagCounts.add(tag,c);
      }
    }
  }

  private void tagIt() throws IOException {
//    FileWriter fw = new FileWriter("/home/denny/proj/NLP/gene.dev.p1.out");
//    File fcount = new File("/home/denny/proj/NLP/gene.dev");
    FileWriter fw = new FileWriter("/home/denny/proj/NLP/gene_test.p1.out");
    File fcount = new File("/home/denny/proj/NLP/gene.test");
    BufferedReader br = new BufferedReader(new FileReader(fcount));
    String line;

    while((line=br.readLine())!=null){
      if( line.equals("") ){
        fw.write("\n");
      }else{
        fw.write(line+" "+tagFor(line)+"\n");
      }
    }
    fw.close();
  }

}
